#include "mainscene.h"
#include "ui_mainscene.h"
#include "gameconfig.h"
#include <QPixmap>
#include <QPainter>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QDebug>
#include <QTimer>
#include "gamepushbutton.h"
#include "gamecontroller.h"
MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    GameController::getInstance()->initMainScene(this);
    ui->setupUi(this);
    this->setWindowTitle(tr("翻金币游戏"));
    this->setWindowIcon(QIcon(GAME_ICON));
    this->setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    //创建菜单栏
    QMenuBar* bar = menuBar();
    setMenuBar(bar);
    //创建开始菜单项
    QMenu* startMenu = bar->addMenu("开始");
    //创建退出 菜单项
    QAction * quitAction = startMenu->addAction("退出");

    //点击退出 实现退出游戏
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });
    //开始按钮
    GamePushButton* startBtn = new GamePushButton(BTN_START_GAME);
    startBtn->setParent(this);
    startBtn->move(this->width()*0.5 - startBtn->width()*0.5,this->height()*0.7);
    connect(startBtn,&GamePushButton::clicked,this,[=](){
        qDebug()<<"点击了开始按钮！";
        GameController::getInstance()->playStartSound();
        startBtn->zoomUp();
        startBtn->zoomDown();
        QTimer::singleShot(500,this,[=](){
            GameController::getInstance()->hideMainScene();
            GameController::getInstance()->showLevelScene();

        });
    });
}
MainScene::~MainScene()
{
    delete ui;
}

void MainScene::paintEvent(QPaintEvent *event){
    QPainter mainPaiter(this);
    QPixmap mainPixmap;
    bool result = mainPixmap.load(IMG_BG_MAINSCENE);
    if(result){
        mainPaiter.drawPixmap(0,0,mainPixmap);
    }
}
